package com.future.utils;

import org.apache.commons.lang3.time.DateUtils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 日期工具类
 */
public final class DateUtil {

    /**
     * 将Date转换为String
     */
    public static String toString(Date date, String pattern) {
        SimpleDateFormat df = new SimpleDateFormat(pattern);
        return df.format(date);
    }

    /**
     * 将Date转换为String，默认格式为yyyy-MM-dd HH-mm-ss
     */
    public static String toString(Date date) {
        return toString(date, "yyyy-MM-dd HH-mm-ss");
    }

    /**
     * 将String转换为Date
     */
    public static Date toDate(String s, String pattern) throws ParseException {
        DateFormat df = new SimpleDateFormat(pattern);
        return df.parse(s);
    }

    /**
     * 将String转换为Date，默认格式为 yyyy-MM-dd HH-mm-ss
     */
    public static Date toDate(String s) throws ParseException {
        return toDate(s, "yyyy-MM-dd HH-mm-ss");
    }

    //1.获取近14天日期
    public static List<String> getSevenDate(int days) {
        List<String> dateList = new ArrayList<>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        for (int i = days-2; i >=-1; i--) {
            Date date = DateUtils.addDays(new Date(), -i);
            String formatDate = sdf.format(date);
            dateList.add(formatDate);
        }
        return dateList;
    }

    public static void main(String[] args) {
        System.out.println(getSevenDate(14));
    }
}

